/**
 * 
 */
package com.htht.smartschool.action.license;

import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.htht.smartschool.action.baseaction.BaseAction;
import com.htht.smartschool.common.model.ParamData;
import com.htht.smartschool.common.servlet.DataContainer;
import com.htht.smartschool.model.card.TPhonebooktemp;
import com.htht.smartschool.model.datarole.DataBean;
import com.htht.smartschool.model.datarole.DataRole;
import com.htht.smartschool.model.datauser.DataUser;
import com.htht.smartschool.model.license.License;
import com.htht.smartschool.model.models.Models;
import com.htht.smartschool.model.roles.Role;
import com.htht.smartschool.service.license.LicenseService;
import com.htht.smartschool.util.system.GsonUtils;

/**
 * @author wuqiwei
 *
 */
public class LicenseAction extends BaseAction{

	@SuppressWarnings("rawtypes")
	@Override
	public boolean processData(DataContainer obj, ParamData pd) {
		boolean flag = false;
		LicenseService licenseService = new LicenseService();
		Role  role = null;
		License license = null;
		DataUser datauser = null;
		Models models = null;
		DataRole datarole = null;
		if(pd.getCmd().equalsIgnoreCase("getRole")){
			role =(Role) GsonUtils.getInstance().fromJson(pd.getData(), Role.class);
			Map map = licenseService.getRole(role);//查看角色
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("查询角色失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateRole")){
			role =(Role) GsonUtils.getInstance().fromJson(pd.getData(), Role.class);
			boolean result = licenseService.updateRole(role);//修改角色
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("修改失败");
			} 
		}else if(pd.getCmd().equalsIgnoreCase("deleteRole")){
			role =(Role) GsonUtils.getInstance().fromJson(pd.getData(), Role.class);
			boolean result =licenseService.deleteRole(role);//删除表t_role信息
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("无法删除");
			}
		}else if(pd.getCmd().equalsIgnoreCase("insertRole")){
			role =(Role) GsonUtils.getInstance().fromJson(pd.getData(), Role.class);
			boolean result = licenseService.insertRole(role);//插入t_role信息
			if(result){
				flag = true;
				obj.setData(result);
			}else {
				obj.setData("插入失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("insertLicenseModule")){
			license = (License) GsonUtils.getInstance().fromJson(pd.getData(), License.class);
			boolean result = licenseService.insertLicense(license);//插入t_license信息
			if(result){
				flag = true;
				obj.setData(result);
			}else {
				obj.setData("插入失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryLicense")){
			license = (License) GsonUtils.getInstance().fromJson(pd.getData(), License.class);
			List<License> list = licenseService.getLicense(license);//查看权限表
			if(list.size() > 0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查询权限失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateLicense")){
			license = (License) GsonUtils.getInstance().fromJson(pd.getData(), License.class);
			boolean result = licenseService.updateLicense(license);//修改权限表
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("修改失败");
			} 
		}else if(pd.getCmd().equalsIgnoreCase("deleteLicense")){
			license = (License) GsonUtils.getInstance().fromJson(pd.getData(), License.class);
			boolean result = licenseService.deleteLicense(license);//删除权限信息
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("无法删除");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getDataUser")){
			datauser = (DataUser) GsonUtils.getInstance().fromJson(pd.getData(), DataUser.class);
			List<DataUser> list = licenseService.getDataUser(datauser);//获取用户数据访问限制表信息
			if(list.size() > 0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查询用户数据访问限制表信息失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateDataUser")){
			datauser = (DataUser) GsonUtils.getInstance().fromJson(pd.getData(), DataUser.class);
			boolean result = licenseService.updateDataUser(datauser);//修改用户数据访问限制表信息
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("修改失败");
			} 
		}else if(pd.getCmd().equalsIgnoreCase("deleteDataUser")){
			datauser = (DataUser) GsonUtils.getInstance().fromJson(pd.getData(), DataUser.class);
			boolean result = licenseService.deleteDataUser(datauser);//删除用户数据访问限制表信息
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("无法删除");
			}
		}else if(pd.getCmd().equalsIgnoreCase("insertDataUser")){
			datauser = (DataUser) GsonUtils.getInstance().fromJson(pd.getData(), DataUser.class);
			boolean result = licenseService.insertDataUser(datauser);//添加用户数据访问限制表信息
			if(result){
				flag = true;
				obj.setData(result);
			}else {
				obj.setData("插入失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getDataRole")){
			datarole = (DataRole) GsonUtils.getInstance().fromJson(pd.getData(), DataRole.class);
			List<DataBean> list = licenseService.getDataRole(datarole);//查询角色数据访问限制表信息
			if(list.size() > 0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查询角色数据访问限制表失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateDataRole")){
			datarole = (DataRole) GsonUtils.getInstance().fromJson(pd.getData(), DataRole.class);
			boolean result = licenseService.updateDataRole(datarole);//修改角色数据访问限制表信息
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("修改失败");
			} 
		}else if(pd.getCmd().equalsIgnoreCase("deleteDataRole")){
			datarole = (DataRole) GsonUtils.getInstance().fromJson(pd.getData(), DataRole.class);
			boolean result = licenseService.deleteDataRole(datarole);//删除角色数据访问限制表信息
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("无法删除");
			}
		}else if(pd.getCmd().equalsIgnoreCase("insertDataRole")){
			datarole = (DataRole) GsonUtils.getInstance().fromJson(pd.getData(), DataRole.class);
			boolean result = licenseService.insertDataRole(datarole);//插入角色数据访问限制表信息
			if(result){
				flag = true;
				obj.setData(result);
			}else {
				obj.setData("插入失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryUserlicenseModel")){
			models =(Models) GsonUtils.getInstance().fromJson(pd.getData(), Models.class);
			List<Models> list = licenseService.queryUserlicenseModel(models);//查询角色权限和模块信息
			if(list.size() > 0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查询角色权限和模块信息失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryModels")){
//			models =(Models) GsonUtils.getInstance().fromJson(pd.getData(), Models.class);
			List<Models> list = licenseService.queryModels();//查询模块一级二级三级
			if(list.size() > 0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查询所有模块信息失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateModels")){
			List<Models> listmodels = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<Models>>(){}.getType());
			boolean f = licenseService.updateModels(listmodels);//查询模块一级二级三级
			if(f){
				flag = true;
				obj.setData(f);
			}else{
				obj.setData("更新模块信息失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateRoleUser")){
			role =(Role) GsonUtils.getInstance().fromJson(pd.getData(), Role.class);
			boolean result = licenseService.updateRoleUser(role);//批量修改角色用户信息
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("修改失败");
			} 
		}else if(pd.getCmd().equalsIgnoreCase("getRoleUser")){
			role =(Role) GsonUtils.getInstance().fromJson(pd.getData(), Role.class);
			Map map = licenseService.getRoleUser(role);//查看角色下面的用户
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("查询角色用户失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deleteRoleUser")){
			role =(Role) GsonUtils.getInstance().fromJson(pd.getData(), Role.class);
			boolean result = licenseService.deleteRoleUser(role);//删除角色用户信息
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("修改失败");
			} 
		}else if(pd.getCmd().equalsIgnoreCase("insertRoleUser")){
			List<Role> roles = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<Role>>(){}.getType());
			boolean result = licenseService.insertRoleUser(roles);//批量添加角色用户信息
			if(result){
				flag = true;
				obj.setData(result);
			}else {
				obj.setData("插入失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("batchAddDataRole")){
			List<DataRole> dataRole =GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<DataRole>>(){}.getType());
			boolean result = licenseService.batchAddDataRole(dataRole);//批量添加权限 角色数据权限
			if(result){
				flag = true;
				obj.setData(result);
			}else {
				obj.setData("插入失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addLicenseRoleModels")){
			List<License> dataRole =GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<License>>(){}.getType());
			boolean result = licenseService.addLicenseRoleModels(dataRole); //添加权限角色模块授权
			if(result){
				flag = true;
				obj.setData(result);
			}else {
				obj.setData("插入失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getZtreeDataRole")){//数据授权 显示班级 没处理数据
			datarole = (DataRole) GsonUtils.getInstance().fromJson(pd.getData(), DataRole.class);
			List<DataRole> list = licenseService.getZtreeDataRole(datarole);
			if(list.size() > 0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查询失败");
			}
		}
		return flag;
	}

}
